package com.spring.gop.proj.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.spring.gop.proj.interfaces.AdminService;
import com.spring.gop.proj.model.User;

@Controller
@RequestMapping("/admin/*")
@SessionAttributes("user")
public class AdminController {
	
	@Autowired AdminService adminService;
	
	@RequestMapping(value="/member/management", method=RequestMethod.GET)
	public void managementForm(Model model) {
		model.addAttribute("list", adminService.allMember());
	}
	
	@RequestMapping(value="/member/management", method=RequestMethod.POST)
	public void management(Model model, HttpServletRequest request, String search_select, String search_text) {
		if(search_select.equals("search_all")) {
			model.addAttribute("list", adminService.searchall(search_text));
		} else if(search_select.equals("search_id")) {
			model.addAttribute("list", adminService.idList(search_text));
		} else if(search_select.equals("search_name")) {
			model.addAttribute("list", adminService.nameList(search_text));
		}
	}
	
	@RequestMapping(value="/member/delMember", method=RequestMethod.GET)
	public String delMember(@ModelAttribute("newUse")User user) {
		adminService.delMember(user.getMember_id());
		return "redirect:/admin/member/management";
	}
}
